const { NotFoundError } = require('./error');
const { Brand, Series, Model, Category, Article, Author } = require('../models');
const getBrand = async (req, res, next) => {
  const {id} = req.params;
  const brand = await Brand.findByPk(id, {
    include: [{model:Series, as: 'series'}]
  });
  if(!brand){
    throw new NotFoundError(req.t('not found', {ns: 'brands'}));
  }
  return brand;
}
const getSeries = async (req, res, next) => {
  const {id} = req.params;
  const series = await Series.findByPk(id);
  if(!series){
    throw new NotFoundError(req.t('not found', {ns: 'series'}));
  }
  return series;
}
const getModel = async (req, res, next) => {
  const {id} = req.params;
  const model = await Model.findByPk(id);
  if(!model){
    throw new NotFoundError(req.t('not found', {ns: 'models'}));
  }
  return model;
}
const getCategory = async (req, res, next) => {
  const {id} = req.params;
  const category = await Category.findByPk(id);
  if(!category){
    throw new NotFoundError(req.t('not found', {ns: 'models'})); //ns
  }
  return category;
}
const getArticle = async (req, res, next) => {
  const {id} = req.params;
  const article = await Article.findByPk(id);
  if(!article){
    throw new NotFoundError(req.t('not found', {ns: 'models'})); //ns
  }
  return article;
}
const getAuthor = async (req, res, next) => {
  const {id} = req.params;
  const author = await Author.findByPk(id);
  if(!author){
    throw new NotFoundError(req.t('not found', {ns: 'models'})); //ns
  }
  return author;
}
module.exports = {
  getBrand,
  getSeries,
  getModel,
  getCategory,
  getArticle,
  getAuthor
}

